<template>
  <div>
    <span>全选:</span>
    <input type="checkbox" v-model="allChecked" />
    <button @click="handleClick">反选</button>
    <ul>
      <li v-for="item in arr" :key="item.name">
        <input type="checkbox" v-model="item.c" />
        <span>{{ item.name }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [
        {
          name: "猪八戒",
          c: false,
        },
        {
          name: "孙悟空",
          c: true,
        },
        {
          name: "唐僧",
          c: false,
        },
        {
          name: "白龙马",
          c: false,
        },
      ],
    };
  },
  computed: {
    allChecked: {
      get() {
        // every - 全选代表数组每一项都为true
        return this.arr.every((item) => item.c);
      },
      set(val) {
        // 全选反选设置计算属性，就是把每一项改成设置的值
        this.arr.forEach((item) => {
          item.c = val;
        });
      },
    },
  },
  methods: {
    handleClick() {
      // 数组遍历每一项取反
      this.arr.forEach((item) => {
        item.c = !item.c;
      });
    },
  },
};
</script>

<style>
</style>